package com.dty.dtycompute.controller;

import java.time.LocalDateTime;
import java.util.Date;

import com.dty.dtycompute.entity.*;
import com.dty.dtycompute.service.*;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;


/**
 * author: zouzhuo
 * Date: 2023/3/31 0031
 * Time: 上午 11:22
 **/
@RestController
@RequestMapping("/Park/compute")
public class RealTimeDataController {

    @Autowired
    private TarchMeterService tarchMeterService;
    @Autowired
    private TdataHtAeqService tdataHtAeqService;
    @Autowired
    private TdataHtCuharmonicService tdataHtCuharmonicService;
    @Autowired
    private TdataHtCuService tdataHtCuService;
    @Autowired
    private TdataHtLvoService tdataHtLvoService;
    @Autowired
    private TdataHtMonmaxdemandService tdataHtMonmaxdemandService;
    @Autowired
    private TdataHtOtherenergyService tdataHtOtherenergyService;
    @Autowired
    private TdataHtPvoService tdataHtPvoService;
    @Autowired
    private TdataHtPwService tdataHtPwService;
    @Autowired
    private TdataHtReqService tdataHtReqService;
    @Autowired
    private TdataHtUnbalanceService tdataHtUnbalanceService;
    @Autowired
    private TdataRtAeqService tdataRtAeqService;
    @Autowired
    private TdataRtCuharmonicService tdataRtCuharmonicService;
    @Autowired
    private TdataHtVoharmonicService tdataHtVoharmonicService;
    @Autowired
    private TdataRtCuService tdataRtCuService;
    @Autowired
    private TdataRtLvoService tdataRtLvoService;
    @Autowired
    private TdataRtMonmaxdemandService tdataRtMonmaxdemandService;
    @Autowired
    private TdataRtOtherenergyService tdataRtOtherenergyService;
    @Autowired
    private TdataRtPvoService tdataRtPvoService;
    @Autowired
    private TdataRtPwService tdataRtPwService;
    @Autowired
    private TdataRtReqService tdataRtReqService;
    @Autowired
    private TdataRtUnbalanceService tdataRtUnbalanceService;
    @Autowired
    private TdataRtVoharmonicService tdataRtVoharmonicService;

    /**
     * 综合能源演示数据
     */
    @PostMapping("/energy/demo")
    public void timeFun() {
        //仪表查询
        List<TarchMeter> tarchMeterList = tarchMeterService.select();
        //5.1.有功电量实时数据表【Tdata_rt_aeq】
        Map<String, TdataRtAeq> tdataRtAeq = tdataRtAeqService.selectTdataRtAeq();
        for (TarchMeter tarchMeter : tarchMeterList) {
            TdataRtAeq tdataRtAeqInfo = tdataRtAeq.get(tarchMeter.getMeterId());
            BigDecimal jian = randomNum(tdataRtAeqInfo.getJian());
            BigDecimal feng = randomNum(tdataRtAeqInfo.getFeng());
            BigDecimal ping = randomNum(tdataRtAeqInfo.getPing());
            BigDecimal gu = tdataRtAeqInfo.getGu();
            BigDecimal zong = jian.add(feng).add(ping).add(gu);
            tdataRtAeqInfo.setZong(zong);
            tdataRtAeqInfo.setJian(jian);
            tdataRtAeqInfo.setFeng(jian);
            tdataRtAeqInfo.setPing(ping);
            tdataRtAeqInfo.setGu(gu);
            int i = tdataRtAeqService.updateTdataRtAeq(tdataRtAeqInfo);
            if (i > 0) {
                System.out.println(tarchMeter.getMeterNo() + "表更新有功电量实时数据表成功");
                tdataRtAeqInfo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                int k = tdataHtAeqService.insertTdataHtAeqInfo(tdataRtAeqInfo);
                if (k > 0) {
                    System.out.println(tarchMeter.getMeterNo() + "表插入有功电量历史数据表成功");
                } else {
                    System.out.println(tarchMeter.getMeterNo() + "表插入有功电量历史数据表失败");
                }
            } else {
                System.out.println(tarchMeter.getMeterNo() + "表更新有功电量实时数据表失败");
            }
        }
        //5.2.无功电量实时数据表【Tdata_rt_req】
        Map<String, TdataRtReq> tdataRtReq = tdataRtReqService.selectTdataRtReq();
        for (TarchMeter tarchMeter : tarchMeterList) {
            TdataRtReq tdataRtReqInfo = tdataRtReq.get(tarchMeter.getMeterId());
            tdataRtReqInfo.setMeterNo(tarchMeter.getMeterNo());
            BigDecimal jian = randomNum(tdataRtReqInfo.getJian());
            BigDecimal feng = randomNum(tdataRtReqInfo.getFeng());
            BigDecimal ping = randomNum(tdataRtReqInfo.getPing());
            BigDecimal gu = randomNum(tdataRtReqInfo.getGu());
            //将BigDecimal类型的jian,feng,ping,gu增加一个5到10之间的随机数,并且将BigDecimal的zong为五者之和
            BigDecimal zong = jian.add(feng).add(ping).add(gu);
            tdataRtReqInfo.setZong(zong);
            tdataRtReqInfo.setJian(jian);
            tdataRtReqInfo.setFeng(jian);
            tdataRtReqInfo.setPing(ping);
            tdataRtReqInfo.setGu(gu);
            int i = tdataRtReqService.updateTdataRtAeq(tdataRtReqInfo);
            if (i > 0) {
                System.out.println(tarchMeter.getMeterNo() + "表更新有功电量实时数据表成功");
                tdataRtReqInfo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                int k = tdataHtReqService.insertTdataHtReqInfo(tdataRtReqInfo);
                if (k > 0) {
                    System.out.println(tarchMeter.getMeterNo() + "表插入有功电量历史数据表成功");
                } else {
                    System.out.println(tarchMeter.getMeterNo() + "表插入有功电量历史数据表失败");
                }
            } else {
                System.out.println(tarchMeter.getMeterNo() + "表更新有功电量实时数据表失败");
            }
        }
//        5.3.功率实时数据表【Tdata_rt_pw】
        Map<String, TdataRtPw> tdataRtPwMap = tdataRtPwService.selectTdataRtPw();
        for (TarchMeter tarchMeter : tarchMeterList) {
            TdataRtPw tdataRtPw = tdataRtPwMap.get(tarchMeter.getMeterId());
            BigDecimal ypwA = activePower(tdataRtPw.getYpwA());
            BigDecimal ypwB = activePower(tdataRtPw.getYpwB());
            BigDecimal ypwC = activePower(tdataRtPw.getYpwC());
            BigDecimal ypw = ypwA.add(ypwB).add(ypwC);
            tdataRtPw.setYpw(ypw);
            tdataRtPw.setYpwA(ypwA);
            tdataRtPw.setYpwB(ypwB);
            tdataRtPw.setYpwC(ypwC);
            BigDecimal wpwA = activePower(tdataRtPw.getWpwA());
            BigDecimal wpwB = activePower(tdataRtPw.getWpwB());
            BigDecimal wpwC = activePower(tdataRtPw.getWpwC());
            BigDecimal wpw = wpwA.add(wpwB).add(wpwC);
            tdataRtPw.setWpw(wpw);
            tdataRtPw.setWpwA(wpwA);
            tdataRtPw.setWpwB(wpwB);
            tdataRtPw.setWpwC(wpwC);
            BigDecimal pwFactorA = powerNum(tdataRtPw.getPwFactorA());
            BigDecimal pwFactorB = powerNum(tdataRtPw.getPwFactorB());
            BigDecimal pwFactorC = powerNum(tdataRtPw.getPwFactorC());
            BigDecimal pwFactor = pwFactorA.add(pwFactorB).add(pwFactorC);
            tdataRtPw.setPwFactorA(pwFactorA);
            tdataRtPw.setPwFactorB(pwFactorB);
            tdataRtPw.setPwFactorC(pwFactorC);
            tdataRtPw.setPwFactor(pwFactor);
            BigDecimal spwA = activePower(tdataRtPw.getSpwA());
            BigDecimal spwB = activePower(tdataRtPw.getSpwB());
            BigDecimal spwC = activePower(tdataRtPw.getSpwA());
            BigDecimal spw = spwA.add(spwB).add(spwC);
            tdataRtPw.setSpw(spw);
            tdataRtPw.setSpwA(spwA);
            tdataRtPw.setSpwB(spwB);
            tdataRtPw.setSpwC(spwC);
            int i = tdataRtPwService.updateTdataRtPw(tdataRtPw);
            if (i > 0) {
                System.out.println(tarchMeter.getMeterNo() + "表更新功率实时数据表成功");
                tdataRtPw.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                int k = tdataHtPwService.insertTdataRtPw(tdataRtPw);
                if (k > 0) {
                    System.out.println(tarchMeter.getMeterNo() + "表插入功率历史数据表成功");
                } else {
                    System.out.println(tarchMeter.getMeterNo() + "表插入功率历史数据表成功");
                }
            } else {
                System.out.println(tarchMeter.getMeterNo() + "表更新功率实时数据表失败");
            }
        }
//        5.4.线电压实时数据表【Tdata_rt_lvo】
        Map<String, TdataRtLvo> tdataRtLvoMap = tdataRtLvoService.selectTdataRtLvo();
        for (TarchMeter tarchMeter : tarchMeterList) {
            TdataRtLvo tdataRtLvo = tdataRtLvoMap.get(tarchMeter.getMeterId());
            tdataRtLvo.setMeterNo(tarchMeter.getMeterNo());
            BigDecimal vab = voltage(tdataRtLvo.getVab());
            BigDecimal vca = voltage(tdataRtLvo.getVca());
            BigDecimal vbc = voltage(tdataRtLvo.getVbc());
            tdataRtLvo.setVab(vab);
            tdataRtLvo.setVca(vca);
            tdataRtLvo.setVbc(vbc);
            int i = tdataRtLvoService.updateTdataRtLvo(tdataRtLvo);
            if (i > 0) {
                System.out.println(tarchMeter.getMeterNo() + "表更新线电压实时数据表成功");
                tdataRtLvo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                tdataRtLvo.setMeterNo(tarchMeter.getMeterNo());
                int k = tdataHtLvoService.insertTdataHtLvo(tdataRtLvo);
                if (k > 0) {
                    System.out.println(tarchMeter.getMeterNo() + "表更新线电压历史数据表成功");
                } else {
                    System.out.println(tarchMeter.getMeterNo() + "表更新线电压历史数据表失败");
                }
            } else {
                System.out.println(tarchMeter.getMeterNo() + "表更新线电压实时数据表失败");
            }
        }
//        5.5.相电压实时数据表【Tdata_rt_pvo】
        Map<String, TdataRtPvo> tdataRtPvoMap = tdataRtPvoService.selectTdataRtPvo();
        for (TarchMeter tarchMeter : tarchMeterList) {
            TdataRtPvo tdataRtPvo = tdataRtPvoMap.get(tarchMeter.getMeterId());
            BigDecimal pvoA = voltage(tdataRtPvo.getPvoA());
            BigDecimal pvoB = voltage(tdataRtPvo.getPvoB());
            BigDecimal pvoC = voltage(tdataRtPvo.getPvoC());
            tdataRtPvo.setPvoA(pvoA);
            tdataRtPvo.setPvoB(pvoB);
            tdataRtPvo.setPvoC(pvoC);
            int i = tdataRtPvoService.updateTdataRtPvo(tdataRtPvo);
            if (i > 0) {
                System.out.println(tarchMeter.getMeterNo() + "表更新相电压实时数据表成功");
                tdataRtPvo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                int k = tdataHtPvoService.insertTdataHtPvo(tdataRtPvo);
                if (k > 0) {
                    System.out.println(tarchMeter.getMeterNo() + "表插入相电压实时数据表成功");
                } else {
                    System.out.println(tarchMeter.getMeterNo() + "表插入相电压实时数据表失败");
                }
            } else {
                System.out.println(tarchMeter.getMeterNo() + "表更新相电压实时数据表失败");
            }
        }
//        5.6.电流实时数据表【Tdata_rt_cu】
        Map<String, TdataRtCu> tdataRtCuMap = tdataRtCuService.selecetTdataRtCu();
        for (TarchMeter tarchMeter : tarchMeterList) {
            TdataRtCu tdataRtCu = tdataRtCuMap.get(tarchMeter.getMeterId());
            BigDecimal cuA = CurrentRealTime(tdataRtCu.getCuA());
            BigDecimal cuB = CurrentRealTime(CurrentRealTime(tdataRtCu.getCuB()));
            BigDecimal cuC = CurrentRealTime(CurrentRealTime(tdataRtCu.getCuC()));
            tdataRtCu.setCuA(cuA);
            tdataRtCu.setCuB(cuB);
            tdataRtCu.setCuC(cuC);
            int i = tdataRtCuService.updataTdataRtCu(tdataRtCu);
            if (i > 0) {
                System.out.println(tarchMeter.getMeterNo() + "表更新电流实时数据表失败");
                tdataRtCu.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                int k = tdataHtCuService.insertTdataHtCu(tdataRtCu);
                if (k > 0) {
                    System.out.println(tarchMeter.getMeterNo() + "表插入电流实时数据表成功");
                } else {
                    System.out.println(tarchMeter.getMeterNo() + "表插入电流历史数据表失败");
                }
            } else {
                System.out.println(tarchMeter.getMeterNo() + "表更新电流实时数据表失败");
            }
        }
//        5.7.电压谐波实时数据表【Tdata_rt_voHarmonic】
        Map<String, TdataRtVoharmonic> tdataRtVoharmonicMap = tdataRtVoharmonicService.selectTdataRtVoharmonic();
        for (TarchMeter tarchMeter : tarchMeterList) {
            TdataRtVoharmonic tdataRtVoharmonic = tdataRtVoharmonicMap.get(tarchMeter.getMeterId());
            BigDecimal hr1 = voltageHarmonics(tdataRtVoharmonic.getHr1());
            BigDecimal hr2 = voltageHarmonics(tdataRtVoharmonic.getHr2());
            BigDecimal hr3 = voltageHarmonics(tdataRtVoharmonic.getHr3());
            BigDecimal hr4 = voltageHarmonics(tdataRtVoharmonic.getHr4());
            BigDecimal hr5 = voltageHarmonics(tdataRtVoharmonic.getHr5());
            BigDecimal hr6 = voltageHarmonics(tdataRtVoharmonic.getHr6());
            BigDecimal hr7 = voltageHarmonics(tdataRtVoharmonic.getHr7());
            BigDecimal hr8 = voltageHarmonics(tdataRtVoharmonic.getHr8());
            BigDecimal hr9 = voltageHarmonics(tdataRtVoharmonic.getHr9());
            BigDecimal hr10 = voltageHarmonics(tdataRtVoharmonic.getHr10());
            BigDecimal hr11 = voltageHarmonics(tdataRtVoharmonic.getHr11());
            BigDecimal hr12 = voltageHarmonics(tdataRtVoharmonic.getHr12());
            BigDecimal hr13 = voltageHarmonics(tdataRtVoharmonic.getHr13());
            BigDecimal hr14 = voltageHarmonics(tdataRtVoharmonic.getHr14());
            BigDecimal hr15 = voltageHarmonics(tdataRtVoharmonic.getHr15());
            BigDecimal hr16 = voltageHarmonics(tdataRtVoharmonic.getHr16());
            BigDecimal hr17 = voltageHarmonics(tdataRtVoharmonic.getHr17());
            BigDecimal hr18 = voltageHarmonics(tdataRtVoharmonic.getHr18());
            BigDecimal hr19 = voltageHarmonics(tdataRtVoharmonic.getHr19());
            BigDecimal hr20 = voltageHarmonics(tdataRtVoharmonic.getHr20());
            BigDecimal hr21 = voltageHarmonics(tdataRtVoharmonic.getHr21());
            BigDecimal hr22 = voltageHarmonics(tdataRtVoharmonic.getHr22());
            BigDecimal hr23 = voltageHarmonics(tdataRtVoharmonic.getHr23());
            BigDecimal hr24 = voltageHarmonics(tdataRtVoharmonic.getHr24());
            BigDecimal hr25 = voltageHarmonics(tdataRtVoharmonic.getHr25());
            BigDecimal hr26 = voltageHarmonics(tdataRtVoharmonic.getHr26());
            BigDecimal hr27 = voltageHarmonics(tdataRtVoharmonic.getHr27());
            BigDecimal hr28 = voltageHarmonics(tdataRtVoharmonic.getHr28());
            BigDecimal hr29 = voltageHarmonics(tdataRtVoharmonic.getHr29());
            BigDecimal hr30 = voltageHarmonics(tdataRtVoharmonic.getHr30());
            BigDecimal hr31 = voltageHarmonics(tdataRtVoharmonic.getHr31());

            tdataRtVoharmonic.setHr1(hr1);
            tdataRtVoharmonic.setHr2(hr2);
            tdataRtVoharmonic.setHr3(hr3);
            tdataRtVoharmonic.setHr4(hr4);
            tdataRtVoharmonic.setHr5(hr5);
            tdataRtVoharmonic.setHr6(hr6);
            tdataRtVoharmonic.setHr7(hr7);
            tdataRtVoharmonic.setHr8(hr8);
            tdataRtVoharmonic.setHr9(hr9);
            tdataRtVoharmonic.setHr10(hr10);
            tdataRtVoharmonic.setHr11(hr11);
            tdataRtVoharmonic.setHr12(hr12);
            tdataRtVoharmonic.setHr13(hr13);
            tdataRtVoharmonic.setHr14(hr14);
            tdataRtVoharmonic.setHr15(hr15);
            tdataRtVoharmonic.setHr16(hr16);
            tdataRtVoharmonic.setHr17(hr17);
            tdataRtVoharmonic.setHr18(hr18);
            tdataRtVoharmonic.setHr19(hr19);
            tdataRtVoharmonic.setHr20(hr20);
            tdataRtVoharmonic.setHr21(hr21);
            tdataRtVoharmonic.setHr22(hr22);
            tdataRtVoharmonic.setHr23(hr23);
            tdataRtVoharmonic.setHr24(hr24);
            tdataRtVoharmonic.setHr25(hr25);
            tdataRtVoharmonic.setHr26(hr26);
            tdataRtVoharmonic.setHr27(hr27);
            tdataRtVoharmonic.setHr28(hr28);
            tdataRtVoharmonic.setHr29(hr29);
            tdataRtVoharmonic.setHr30(hr30);
            tdataRtVoharmonic.setHr31(hr31);
            int i = tdataRtVoharmonicService.updataTdataRtVo(tdataRtVoharmonic);
            if (i > 0) {
                System.out.println(tarchMeter.getMeterNo() + "表更新电压谐波实时数据表成功");
                tdataRtVoharmonic.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                int k = tdataHtVoharmonicService.insertTdatHtVo(tdataRtVoharmonic);
                if (k > 0) {
                    System.out.println(tarchMeter.getMeterNo() + "表插入电压谐波历史数据表成功");
                } else {
                    System.out.println(tarchMeter.getMeterNo() + "表插入电压谐波历史数据表失败");
                }
            } else {
                System.out.println(tarchMeter.getMeterNo() + "表更新电流实时数据表失败");
            }
        }
//        5.8.电流谐波实时数据表【Tdata_rt_cuHarmonic】
        Map<String, TdataRtCuharmonic> tdataRtCuharmonicMap = tdataRtCuharmonicService.tdataRtCuharmonic();
        for (TarchMeter tarchMeter : tarchMeterList) {
            TdataRtCuharmonic tdataRtCuharmonic = tdataRtCuharmonicMap.get(tarchMeter.getMeterId());
            BigDecimal hr1 = voltageHarmonics(tdataRtCuharmonic.getHr1());
            BigDecimal hr2 = voltageHarmonics(tdataRtCuharmonic.getHr2());
            BigDecimal hr3 = voltageHarmonics(tdataRtCuharmonic.getHr3());
            BigDecimal hr4 = voltageHarmonics(tdataRtCuharmonic.getHr4());
            BigDecimal hr5 = voltageHarmonics(tdataRtCuharmonic.getHr5());
            BigDecimal hr6 = voltageHarmonics(tdataRtCuharmonic.getHr6());
            BigDecimal hr7 = voltageHarmonics(tdataRtCuharmonic.getHr7());
            BigDecimal hr8 = voltageHarmonics(tdataRtCuharmonic.getHr8());
            BigDecimal hr9 = voltageHarmonics(tdataRtCuharmonic.getHr9());
            BigDecimal hr10 = voltageHarmonics(tdataRtCuharmonic.getHr10());
            BigDecimal hr11 = voltageHarmonics(tdataRtCuharmonic.getHr11());
            BigDecimal hr12 = voltageHarmonics(tdataRtCuharmonic.getHr12());
            BigDecimal hr13 = voltageHarmonics(tdataRtCuharmonic.getHr13());
            BigDecimal hr14 = voltageHarmonics(tdataRtCuharmonic.getHr14());
            BigDecimal hr15 = voltageHarmonics(tdataRtCuharmonic.getHr15());
            BigDecimal hr16 = voltageHarmonics(tdataRtCuharmonic.getHr16());
            BigDecimal hr17 = voltageHarmonics(tdataRtCuharmonic.getHr17());
            BigDecimal hr18 = voltageHarmonics(tdataRtCuharmonic.getHr18());
            BigDecimal hr19 = voltageHarmonics(tdataRtCuharmonic.getHr19());
            BigDecimal hr20 = voltageHarmonics(tdataRtCuharmonic.getHr20());
            BigDecimal hr21 = voltageHarmonics(tdataRtCuharmonic.getHr21());
            BigDecimal hr22 = voltageHarmonics(tdataRtCuharmonic.getHr22());
            BigDecimal hr23 = voltageHarmonics(tdataRtCuharmonic.getHr23());
            BigDecimal hr24 = voltageHarmonics(tdataRtCuharmonic.getHr24());
            BigDecimal hr25 = voltageHarmonics(tdataRtCuharmonic.getHr25());
            BigDecimal hr26 = voltageHarmonics(tdataRtCuharmonic.getHr26());
            BigDecimal hr27 = voltageHarmonics(tdataRtCuharmonic.getHr27());
            BigDecimal hr28 = voltageHarmonics(tdataRtCuharmonic.getHr28());
            BigDecimal hr29 = voltageHarmonics(tdataRtCuharmonic.getHr29());
            BigDecimal hr30 = voltageHarmonics(tdataRtCuharmonic.getHr30());
            BigDecimal hr31 = voltageHarmonics(tdataRtCuharmonic.getHr31());

            tdataRtCuharmonic.setHr1(hr1);
            tdataRtCuharmonic.setHr2(hr2);
            tdataRtCuharmonic.setHr3(hr3);
            tdataRtCuharmonic.setHr4(hr4);
            tdataRtCuharmonic.setHr5(hr5);
            tdataRtCuharmonic.setHr6(hr6);
            tdataRtCuharmonic.setHr7(hr7);
            tdataRtCuharmonic.setHr8(hr8);
            tdataRtCuharmonic.setHr9(hr9);
            tdataRtCuharmonic.setHr10(hr10);
            tdataRtCuharmonic.setHr11(hr11);
            tdataRtCuharmonic.setHr12(hr12);
            tdataRtCuharmonic.setHr13(hr13);
            tdataRtCuharmonic.setHr14(hr14);
            tdataRtCuharmonic.setHr15(hr15);
            tdataRtCuharmonic.setHr16(hr16);
            tdataRtCuharmonic.setHr17(hr17);
            tdataRtCuharmonic.setHr18(hr18);
            tdataRtCuharmonic.setHr19(hr19);
            tdataRtCuharmonic.setHr20(hr20);
            tdataRtCuharmonic.setHr21(hr21);
            tdataRtCuharmonic.setHr22(hr22);
            tdataRtCuharmonic.setHr23(hr23);
            tdataRtCuharmonic.setHr24(hr24);
            tdataRtCuharmonic.setHr25(hr25);
            tdataRtCuharmonic.setHr26(hr26);
            tdataRtCuharmonic.setHr27(hr27);
            tdataRtCuharmonic.setHr28(hr28);
            tdataRtCuharmonic.setHr29(hr29);
            tdataRtCuharmonic.setHr30(hr30);
            tdataRtCuharmonic.setHr31(hr31);
            int i = tdataRtCuharmonicService.updataTdataRtCuharmonic(tdataRtCuharmonic);
            if (i > 0) {
                System.out.println(tarchMeter.getMeterNo() + "表更新电流谐波实时数据表成功");
                tdataRtCuharmonic.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                int k = tdataHtCuharmonicService.insertTdatHtVo(tdataRtCuharmonic);
                if (k > 0) {
                    System.out.println(tarchMeter.getMeterNo() + "表插入电流谐波历史数据表成功");
                } else {
                    System.out.println(tarchMeter.getMeterNo() + "表插入电流谐波历史数据表失败");
                }
            } else {
                System.out.println(tarchMeter.getMeterNo() + "表更新电流谐波实时数据表失败");
            }
        }
//        5.9.三相不平衡实时数据表【Tdata_rt_unbalance】
        Map<String, TdataRtUnbalance> tdataRtUnbalanceMap = tdataRtUnbalanceService.selectTdataRtUn();
        for (TarchMeter tarchMeter : tarchMeterList) {
            TdataRtUnbalance tdataRtUnbalance = tdataRtUnbalanceMap.get(tarchMeter.getMeterId());

            BigDecimal vob = voltageHarmonics(tdataRtUnbalance.getVob());
            BigDecimal cub = voltageHarmonics(tdataRtUnbalance.getCub());
            tdataRtUnbalance.setVob(vob);
            tdataRtUnbalance.setCub(cub);
            int i = tdataRtUnbalanceService.updateTdataRun(tdataRtUnbalance);
            if (i > 0) {
                System.out.println(tarchMeter.getMeterNo() + " 表更新三相不平衡实时数据表成功");
                tdataRtUnbalance.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                int k = tdataHtUnbalanceService.insertTdataHtUn(tdataRtUnbalance);
                if (k > 0) {
                    System.out.println(tarchMeter.getMeterNo() + " 表更新三相不平衡历史数据表成功");
                } else {
                    System.out.println(tarchMeter.getMeterNo() + " 表更新三相不平衡历史数据表失败");
                }
            } else {
                System.out.println(tarchMeter.getMeterNo() + " 表更新三相不平衡实时数据表失败");
            }
        }
//        6.10.月冻结最大需量实时数据表【Tdata_rt_monMaxdemand】
        Map<String, TdataRtMonmaxdemand> tdataRtMonmaxdemandMap = tdataRtMonmaxdemandService.selectTdataRtMon();
        for (TarchMeter tarchMeter : tarchMeterList) {
            TdataRtMonmaxdemand tdataRtMonmaxdemand = tdataRtMonmaxdemandMap.get(tarchMeter.getMeterId());
            BigDecimal maxygdm = lunarFreeze(tdataRtMonmaxdemand.getMaxYgDm());
            BigDecimal maxwgdm = lunarFreeze(tdataRtMonmaxdemand.getMaxWgDm());
            tdataRtMonmaxdemand.setMaxYgDm(maxygdm);
            tdataRtMonmaxdemand.setMaxWgDm(maxwgdm);
            tdataRtMonmaxdemand.setMaxYgDmTm(LocalDateTime.now().toString());
            tdataRtMonmaxdemand.setMaxWgDmTm(LocalDateTime.now().toString());
            int i = tdataRtMonmaxdemandService.updateTdataRtMon(tdataRtMonmaxdemand);
            if (i > 0){
                System.out.println(tarchMeter.getMeterNo() + " 表更新月冻结最大需量实时数据表成功");
                tdataRtMonmaxdemand.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                int k = tdataHtMonmaxdemandService.insertTdataHtMon(tdataRtMonmaxdemand);
            } else {
                System.out.println(tarchMeter.getMeterNo() + " 表更新月冻结最大需量实时数据表失败");
            }
        }


    }

    public BigDecimal randomNum(BigDecimal num) {
        return num.add(BigDecimal.valueOf((Math.random() * 5 + 5)));
    }

    public BigDecimal powerNum(BigDecimal num) {
        Random random = new Random();
        return BigDecimal.valueOf(random.nextDouble());
    }

    public BigDecimal activePower(BigDecimal num) {
        Random random = new Random();
        return new BigDecimal(random.nextInt(5001) + 2000);
    }

    public BigDecimal voltage(BigDecimal num) {
        Random random = new Random();
        return new BigDecimal(random.nextInt(7) + 218);
    }

    public BigDecimal CurrentRealTime(BigDecimal num) {
        Random random = new Random();
        return BigDecimal.valueOf(random.nextDouble() * 4 + 1);
    }

    public BigDecimal voltageHarmonics(BigDecimal num) {
        Random random = new Random();
        return BigDecimal.valueOf(random.nextDouble() * 100);
    }
    public BigDecimal lunarFreeze(BigDecimal num) {
        double random = Math.random() * 1000 + 2000;
        return num.add(BigDecimal.valueOf(random));
    }
}
